package yf.mall.controller.api;

import com.github.pagehelper.PageHelper;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.common.Const;
import yf.mall.dao.AnswerDao;
import yf.mall.entity.AnswerEntity;
import yf.mall.entity.MallGoodsEntity;
import yf.mall.entity.QuestionEntity;
import yf.mall.entity.ShopEntity;
import yf.mall.enums.AnswerStatusEnum;
import yf.mall.enums.MallGoodsShelfEnum;
import yf.mall.enums.QuestionStatusEnum;
import yf.mall.service.AnswerService;
import yf.mall.service.MallGoodsService;
import yf.mall.service.QuestionService;
import yf.mall.service.ShopService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 百问百答
 */
@RequestMapping("api/QA")
@RestController
public class ApiQuesAnsController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private AnswerDao answerDao;
    @Autowired
    private MallGoodsService goodsService;
    @Autowired
    private ShopService shopService;

    /**
     * 获取产品的百问百答
     * @param goodsId
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("queryGoodsId")
    public R getQuestion(Long goodsId,Integer page,Integer limit){
        Integer total = questionService.totalQuestion(goodsId);  //总问题数
        PageHelper.startPage(page,limit);
        List<QuestionEntity> questions = questionService.queryByGoodsId(goodsId, QuestionStatusEnum.YES.getIndex());
        questions = answerService.getQuestion(questions);
        MallGoodsEntity goodsEntity = goodsService.get(goodsId);
        return R.ok(questions).putExtra("total",total)
                .putExtra("img",goodsEntity.getImg()).putExtra("goodsName",goodsEntity.getName())
                .putExtra("goodsId",goodsEntity.getId());
    }

    /**
     * 查看百问百答
     * @param queryQuestionId
     * @return
     */
    @RequestMapping("queryQuestionId")
    public R queryByQuestionId(Long queryQuestionId){
        QuestionEntity question = questionService.get(queryQuestionId);
        Integer total = questionService.totalQuestion(question.getGoodsId());  //总问题数
        Integer answerCount = 0;
        if (question != null && question.getStatus() == QuestionStatusEnum.YES.getIndex()){
            List<AnswerEntity> answerEntities = this.answerDao.queryByQuestionId(question.getId(),null);
            if (answerEntities != null && answerEntities.size() > 0){
                answerCount = answerEntities.size();
                answerEntities = answerEntities.stream().filter(answer -> answer.getStatus() == AnswerStatusEnum.YES.getIndex()).collect(Collectors.toList());
                question.setList(answerEntities);
            }
        }
        ShopEntity shop = new ShopEntity();
        shop.setName("社集优选自营店");
        shop.setAvatar("1603263922406WechatIMG110.png");
        MallGoodsEntity goodsEntity = goodsService.get(question.getGoodsId());
        if (goodsEntity.getUserId() != null && !goodsEntity.getUserId().equals(Const.ADMIN_ID)){
            ShopEntity shopEntity = shopService.findByUserId(goodsEntity.getUserId());
            shop = shopEntity;
        }
        return R.ok(question).putExtra("img",goodsEntity.getImg()).putExtra("goodsName",goodsEntity.getName())
                .putExtra("shop",shop).putExtra("goodsId",goodsEntity.getId()).putExtra("total",total).putExtra("answerCount",answerCount);
    }

    /**
     * 提问
     * @param
     * @return
     */
    @RequestMapping("question")
    public R question(@LoginUserId Long userId,Long goodsId,String content){
        if (content == null || content.equals("")){
            throw new RRException("内容不合法");
        }
        MallGoodsEntity goodsEntity = goodsService.get(goodsId);
        if (goodsEntity == null || goodsEntity.getShelf() != MallGoodsShelfEnum.ON.getIndex()){
            throw new RRException("产品不存在或已下架");
        }

        QuestionEntity questionEntity = questionService.apiSave(userId, goodsId, content);
        return R.ok(questionEntity);
    }

    /**
     * 回答
     * @param
     * @return
     */
    @RequestMapping("answer")
    public R answer(@LoginUserId Long userId,Long questionId,String content){
        if (content == null || content.equals("")){
            throw new RRException("内容不合法");
        }
        QuestionEntity question = questionService.get(questionId);
        MallGoodsEntity goodsEntity = goodsService.get(question.getGoodsId());
        //公司产品只能后台回复
        if (goodsEntity.getUserId() == null || goodsEntity.getUserId().equals(Const.ADMIN_ID)){
            throw new RRException("非法操作");
        }
        if (goodsEntity == null || goodsEntity.getShelf() != MallGoodsShelfEnum.ON.getIndex()){
            throw new RRException("产品不存在或已下架");
        }
        // 第三方产品只能是店主才有权限回答
        if (!userId.equals(goodsEntity.getUserId())){
            throw new RRException("权限不足");
        }

        AnswerEntity answerEntity = answerService.apiSave(userId, question, content);
        return R.ok(answerEntity);
    }

    /**
     * 我的问答
     * @param
     * @return
     */
    @RequestMapping("myQuiz")
    public R myQuiz(@LoginUserId Long userId,Integer temp,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<QuestionEntity> question = questionService.myQuiz(userId,temp);
        return R.ok(question);
    }
}
